/*
 * @Author: zpz
 * @Date: 2019-08-12 16:54:03
 * @LastEditors: Bingo
 * @Email: 13714943895@163.com
 */
import {
  SIDEBAR_TYPE,
  DEFAULT_THEME,
  DEFAULT_LAYOUT_MODE,
  DEFAULT_COLOR,
  DEFAULT_COLOR_WEAK,
  DEFAULT_FIXED_HEADER,
  DEFAULT_FIXED_SIDEMENU,
  DEFAULT_FIXED_HEADER_HIDDEN,
  DEFAULT_CONTENT_WIDTH_TYPE,
  DEFAULT_MULTI_TAB,
} from "@/config/const";
import { message } from "ant-design-vue";
import { useStorage } from "@vueuse/core";
// import { favoriteMenus, favoriteMenusList, deleteFavoriteMenus } from '@/api/basicData'
const app = {
  namespaced: true,
  state: {
    sidebar: true,
    device: "desktop",
    screen: "xl",
    theme: "",
    layout: "",
    contentWidth: "",
    fixedHeader: false,
    fixSiderbar: false,
    autoHideHeader: false,
    color: null,
    weak: false,
    multiTab: true,
    resDetail: null,
    // 页面跳转方式，multiTab，jsChange, removeTab，可以用于控制页面是否刷新，jsChange 表示该页面刷新
    pageSkipping: {
      jumpType: "jsChange",
      canSet: true,
    },
    dicObj: {},
    dicLimitObj: {},
    menuDataSource: [],
    key: false, // 全局锁 控制重新登录弹框
  },
  mutations: {
    // 设置收藏的菜单
    SET_MENU_DATA: (state: { menuDataSource: never[] }, newData = []) => {
      state.menuDataSource = [...newData];
    },
    OPEN_KEY: (state: { key: boolean }) => {
      state.key = true;
    },
    CLOSE_KEY: (state: { key: boolean }) => {
      state.key = false;
    },
    SET_SIDEBAR_TYPE: (state: { sidebar: any }, type: any) => {
      state.sidebar = type;
      useStorage(SIDEBAR_TYPE, type);
    },
    CLOSE_SIDEBAR: (state: { sidebar: boolean }) => {
      useStorage(SIDEBAR_TYPE, true);
      state.sidebar = false;
    },
    TOGGLE_DEVICE: (state: { device: any }, device: any) => {
      state.device = device;
    },
    TOGGLE_SCREEN: (state: { screen: any }, screen: any) => {
      // console.log('%c screen ——>', 'color: #00be3c;', screen)
      state.screen = screen;
    },
    TOGGLE_THEME: (state: { theme: any }, theme: any) => {
      // setStore('_DEFAULT_THEME', theme)
      useStorage(DEFAULT_THEME, theme);
      state.theme = theme;
    },
    TOGGLE_LAYOUT_MODE: (state: { layout: any }, layout: any) => {
      useStorage(DEFAULT_LAYOUT_MODE, layout);
      state.layout = layout;
    },
    TOGGLE_FIXED_HEADER: (state: { fixedHeader: any }, fixed: any) => {
      useStorage(DEFAULT_FIXED_HEADER, fixed);
      state.fixedHeader = fixed;
    },
    TOGGLE_FIXED_SIDERBAR: (state: { fixSiderbar: any }, fixed: any) => {
      useStorage(DEFAULT_FIXED_SIDEMENU, fixed);
      state.fixSiderbar = fixed;
    },
    TOGGLE_FIXED_HEADER_HIDDEN: (state: { autoHideHeader: any }, show: any) => {
      useStorage(DEFAULT_FIXED_HEADER_HIDDEN, show);
      state.autoHideHeader = show;
    },
    TOGGLE_CONTENT_WIDTH: (state: { contentWidth: any }, type: any) => {
      useStorage(DEFAULT_CONTENT_WIDTH_TYPE, type);
      state.contentWidth = type;
    },
    TOGGLE_COLOR: (state: { color: any }, color: any) => {
      useStorage(DEFAULT_COLOR, color);
      state.color = color;
    },
    TOGGLE_WEAK: (state: { weak: any }, flag: any) => {
      useStorage(DEFAULT_COLOR_WEAK, flag);
      state.weak = flag;
    },
    TOGGLE_MULTI_TAB: (state: { multiTab: any }, bool: any) => {
      useStorage(DEFAULT_MULTI_TAB, bool);
      state.multiTab = bool;
    },
    SET_RESDETAIL: (state: { resDetail: any }, obj: any) => {
      state.resDetail = obj;
    },
    SET_PAGE_SKIPPING: (
      state: { pageSkipping: { jumpType: any; canSet: any } },
      obj: { jumpType: any; canSet: any }
    ) => {
      state.pageSkipping = {
        jumpType: obj.jumpType,
        canSet: obj.canSet,
      };
    },
    SET_DIC: (state: { dicObj: {} }, obj: any) => {
      if (obj === "refresh") {
        state.dicObj = {};
      } else {
        state.dicObj = {
          ...state.dicObj,
          ...obj,
        };
      }
    },
    SET_LIMIT_DIC: (state: { dicLimitObj: {} }, obj: any) => {
      if (obj === "refresh") {
        state.dicLimitObj = {};
      } else {
        state.dicLimitObj = {
          ...state.dicLimitObj,
          ...obj,
        };
      }
    },
  },
  actions: {
    // 增加菜单收藏
    // async setMenuData({ commit }, data) {
    //   // 调用api await setMenuData
    //   try {
    //     await favoriteMenus(data)
    //     let res = await favoriteMenusList({ dto: {} })
    //     message.success('收藏成功')
    //     commit('SET_MENU_DATA', [...res.content])
    //   } catch (error) {
    //     console.log('error', error)
    //   }

    // },
    // // 查询所有收藏的页面
    // async searchAllMenuData({ commit }, data) {
    //   try {
    //     let res = await favoriteMenusList({ dto: {} })
    //     commit('SET_MENU_DATA', [...res.content])
    //   } catch (error) {
    //     console.log('error', error)
    //   }
    // },
    // // 删除菜单收藏
    // async deleteMenuData({ commit }, data) {
    //   try {
    //     await deleteFavoriteMenus(data)
    //     let res = await favoriteMenusList({ dto: {} })
    //     message.success('取消收藏成功')
    //     commit('SET_MENU_DATA', [...res.content])
    //   } catch (error) {
    //     console.log('error', error)
    //   }
    // },
    setSidebar({ commit }: any, type: any) {
      commit("SET_SIDEBAR_TYPE", type);
    },
    CloseSidebar({ commit }: any) {
      commit("CLOSE_SIDEBAR");
    },
    ToggleDevice({ commit }: any, device: any) {
      commit("TOGGLE_DEVICE", device);
    },
    ToggleTheme({ commit }: any, theme: any) {
      commit("TOGGLE_THEME", theme);
    },
    ToggleLayoutMode({ commit }: any, mode: any) {
      commit("TOGGLE_LAYOUT_MODE", mode);
    },
    ToggleFixedHeader({ commit }: any, fixedHeader: any) {
      if (!fixedHeader) {
        commit("TOGGLE_FIXED_HEADER_HIDDEN", false);
      }
      commit("TOGGLE_FIXED_HEADER", fixedHeader);
    },
    ToggleFixSiderbar({ commit }: any, fixSiderbar: any) {
      commit("TOGGLE_FIXED_SIDERBAR", fixSiderbar);
    },
    ToggleFixedHeaderHidden({ commit }: any, show: any) {
      commit("TOGGLE_FIXED_HEADER_HIDDEN", show);
    },
    ToggleContentWidth({ commit }: any, type: any) {
      commit("TOGGLE_CONTENT_WIDTH", type);
    },
    ToggleColor({ commit }: any, color: any) {
      commit("TOGGLE_COLOR", color);
    },
    ToggleWeak({ commit }: any, weakFlag: any) {
      commit("TOGGLE_WEAK", weakFlag);
    },
    ToggleMultiTab({ commit }: any, bool: any) {
      commit("TOGGLE_MULTI_TAB", bool);
    },
  },
};

export default app;
